package com.jkc.xh.fragments;

import android.content.ClipboardManager;
import android.content.Context;
import android.webkit.WebView;

import com.jkc.xh.MainApplication;
import com.jkc.xh.R;
import com.jkc.xh.fragments.core.BaseFragment;
import com.jkc.xh.manager.UserManager;
import com.jkc.xh.models.BankCardModel;
import com.jkc.xh.utils.CacheTools;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.LongClick;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

/**
 * 付款方式
 * Created by zhangbreeze on 15/11/21.
 */
@EFragment(R.layout.fragment_pay_alibaba)
public class PayWayAliFragment extends BaseFragment {
    /*@ViewById(R.id.pay_ali_icon)
    ImageView pay_ali_icon;
    @ViewById(R.id.aliQcCode)
    ImageView aliQcCode;
    @ViewById(R.id.accountName)
    TextView accountName;
    @ViewById(R.id.accountNumber)
    TextView accountNumber;
    @ViewById
    TextView hintInfo;*/
    @ViewById(R.id.webview)
    WebView mWebView;
    private ClipboardManager myClipboard;

    @AfterViews
    public void initView() {
        myClipboard = (ClipboardManager) MainApplication.getInstance().getSystemService(Context.CLIPBOARD_SERVICE);

        /*addJob(UserManager.getCpmfHintConfig(new RestCallBack<SystemHintResponse>() {
            @Override
            public void success(SystemHintResponse obj) {
                hintInfo.setText(Html.fromHtml(obj.alipay_remark));
            }

            @Override
            public void failure(RestError error) {

            }
        }));*/
        String url = CacheTools.getPageUrl() + "/Index/nest/payapp/id/{sid}.html".replace("{sid}", UserManager.getSid());
        mWebView.loadUrl(url);
    }

    public void loadCardData(ArrayList<BankCardModel> datas) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        /*BankCardModel model = datas.get(0);
        ImageLoader.loadImage(model.logo, pay_ali_icon);
        ImageLoader.loadImage(model.orcode, aliQcCode);
        accountName.setText(model.bank_realname);
        accountNumber.setText(model.bank_card);
        accountName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myClipboard.setText(accountName.getText());
                ToastUtil.showToastShort("已经复制到黏贴板!");
            }
        });
        accountNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myClipboard.setText(accountNumber.getText());
                ToastUtil.showToastShort("已经复制到黏贴板!");
            }
        });*/
    }

    @LongClick(R.id.aliQcCode)
    public void saveQcCode() {
        /*try {
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()).setTitle(R.string.str_dialog_title).setMessage("是否保存二维码");
            dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    aliQcCode.setDrawingCacheEnabled(true);
                    saveImageToGallery(getActivity(), aliQcCode.getDrawingCache());
                    aliQcCode.setDrawingCacheEnabled(false);
                }
            }).create().show();
        } catch (Exception e) {

        }*/
    }


//    private static void saveImageToGallery(Context context, Bitmap bmp) {
//        // 首先保存图片
//        File appDir = new File(Environment.getExternalStorageDirectory(), "CPMF");
//        if (!appDir.exists()) {
//            appDir.mkdir();
//        }
//        String fileName = System.currentTimeMillis() + ".jpg";
//        File file = new File(appDir, fileName);
//        try {
//            FileOutputStream fos = new FileOutputStream(file);
//            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
//            fos.flush();
//            fos.close();
//            ToastUtil.showToastShort("图片已保存至SD卡下的CPMF目录中");
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        // 其次把文件插入到系统图库
//        try {
//            MediaStore.Images.Media.insertImage(context.getContentResolver(),
//                    file.getAbsolutePath(), fileName, null);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        // 最后通知图库更新
//        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
//    }
}
